//
//  AppDelegate.swift
//  XingCeHelper
//
//  Created by 申铭 on 2022/7/27.
//

import UIKit
import SVProgressHUD
import Bugly

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var currentBrightness: CGFloat = UIScreen.main.brightness

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = MainTabBarController()
        window?.makeKeyAndVisible()
        
//        // 强制关闭暗黑模式
//        if #available(iOS 13.0, *) {
//            window?.overrideUserInterfaceStyle = .light
//        }
        
        SVProgressHUD.setDefaultStyle(.dark)
        SVProgressHUD.setMinimumDismissTimeInterval(0.2)
        SVProgressHUD.setMaximumDismissTimeInterval(0.2)
        SVProgressHUD.setOffsetFromCenter(UIOffset(horizontal: 0, vertical: 90))
        
        ETKeyboardHandler.shared.setup()
        Bugly.start(withAppId: "3d13f55761")
        
        return true
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        let vc = UIViewController.currentViewController()
        if !(vc is TimingViewController || vc is TimingSettingsController) {
            return
        }
        
        let flag = UserDefaults.standard.bool(forKey: kUserDefaults_BrightnessSwitch)
        if flag {
            let value = UserDefaults.standard.float(forKey: kUserDefaults_BrightnessValue)
            UIScreen.main.brightness = CGFloat(value)
        }
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        let vc = UIViewController.currentViewController()
        if !(vc is TimingViewController || vc is TimingSettingsController) {
            return
        }
        
        let flag = UserDefaults.standard.bool(forKey: kUserDefaults_BrightnessSwitch)
        if flag {
            UIScreen.main.brightness = currentBrightness
        }
    }
    
    func applicationWillTerminate(_ application: UIApplication) {
        
    }
    
    // scheme url
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        openUrl(url: url)
        return true
    }
    
    /// 快捷启动
    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        
    }
    
    // MARK: - private
    
    private func openUrl(url: URL?) {
        guard let url = url else {
            print("url空")
            return
        }
        
        print("url:\(url)")
        
        if url.scheme == "file" {
            let vc = ImportFileDataController()
            vc.fileUrl = url
            UIViewController.currentNavController()?.pushViewController(vc, animated: true)
            return
        }
        
        var type = TimeStatisticsType.xingce
        if url.absoluteString.contains("timing/xingce") {
            type = .xingce
        } else if url.absoluteString.contains("timing/ziliaofenxi") {
            type = .ziliaofenxi
        } else if url.absoluteString.contains("timing/shenlun/lianti") {
            type = .shenlunlianti
        } else if url.absoluteString.contains("timing/shenlun/mokao") {
            type = .shenlun
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.8, execute: {
            let vc = TimingViewController()
            vc.type = type
            UIViewController.currentNavController()?.pushViewController(vc, animated: true)
        })
    }
}

/// debug下输出
func MyLog<T>(_ message : T, fileName : String = #file, methodName : String = #function, lineNumber : Int = #line)
{
    print("\(fileName).\(methodName)[\(lineNumber)]:\(message)")
//    #if MYLOG
//    let filename = (fileName as NSString).pathComponents.last
//    DDLogInfo("\(filename!).\(methodName)[\(lineNumber)]:\(message)")
//    #endif
}
